﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace SaaS
{
    public partial class PublishWorkflow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void clearEdit()
        {
            lblWorkflowID.Text = "";
            lblWorkflowName.Text = "";
            ddlServices.Items.Clear();
            ddlServices.Enabled = false;
            Button2.Enabled = false;
            gvServices.DataSourceID = "";
            gvServices.DataBind();
        }

        protected void editWorkflow(string workflowID, string workflowName)
        {
            ddlServices.Items.Clear();
            ddlServices.Enabled = true;
            Button2.Enabled = true;
            MiniSaaSDB db = new MiniSaaSDB();
            SqlDataReader reader;
            string servicesInWorkflow;
            db.open();
            lblWorkflowID.Text = workflowID;
            lblWorkflowName.Text = workflowName;
            servicesInWorkflow = "(SELECT s.Service_ID FROM SERVICE s " +
                "INNER JOIN SYSTEM_WORKFLOW_SERVICE sws ON " +
                "s.Service_ID = sws.Service_ID WHERE sws.System_Workflow_ID = " +
                workflowID + ")";
            reader = db.executeQuery("SELECT * FROM SERVICE WHERE Login_ID IS NULL AND " +
                "Service_ID NOT IN " + servicesInWorkflow + ";");
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    ddlServices.Items.Add(reader["Service_ID"].ToString() + ". " +
                        reader["Service_Name"].ToString());
                }
            }
            reader.Close();
            db.close();
            SqlDataSource2.SelectCommand = "(SELECT * FROM SERVICE s " +
                "INNER JOIN SYSTEM_WORKFLOW_SERVICE sws ON " +
                "s.Service_ID = sws.Service_ID WHERE sws.System_Workflow_ID = " +
                workflowID + ")"; 
            gvServices.DataSourceID = "SqlDataSource2";
            gvServices.DataBind();
        }

        protected void addServiceToWorkflow(string workflowID, string serviceID)
        {
            MiniSaaSDB db = new MiniSaaSDB();
            SqlDataReader reader;
            db.open();
            reader = db.executeQuery("SELECT * FROM SYSTEM_WORKFLOW_SERVICE WHERE Service_ID = " + serviceID + " AND System_Workflow_ID = " + workflowID + ";");
            if (reader.HasRows)
                return;
            else
            {
                reader.Close();
                db.executeQuery("INSERT INTO SYSTEM_WORKFLOW_SERVICE (Service_ID, System_Workflow_ID) VALUES (" + serviceID + ", " + workflowID + ");");
            }
            db.close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (ddlServices.Text.Equals("") || lblWorkflowID.Text.Equals(""))
                return;
            string serviceID = ddlServices.Text.Substring(0, ddlServices.Text.IndexOf('.'));
            addServiceToWorkflow(lblWorkflowID.Text, serviceID);
            ddlServices.Items.RemoveAt(ddlServices.SelectedIndex);
            refreshServiceList();
        }

        protected void refreshServiceList()
        {
            SqlDataSource2.SelectCommand = "(SELECT * FROM SERVICE s " +
                "INNER JOIN SYSTEM_WORKFLOW_SERVICE sws ON " +
                "s.Service_ID = sws.Service_ID WHERE sws.System_Workflow_ID = " +
                lblWorkflowID.Text + ")";
            gvServices.DataSourceID = "SqlDataSource2";
            gvServices.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MiniSaaSDB db = new MiniSaaSDB();
            SqlDataReader reader;
            string nextID;
            db.open();
            reader = db.executeQuery("SELECT MAX(System_Workflow_ID)+1 FROM SYSTEM_WORKFLOW");
            if (!reader.HasRows)
                nextID = "1";
            else
            {
                reader.Read();
                if (reader[0] == null || reader[0].ToString().Equals(""))
                    nextID = "1";
                else
                    nextID = reader[0].ToString();
            }
            reader.Close();
            db.executeNonQuery("INSERT INTO SYSTEM_WORKFLOW (System_Workflow_ID, System_Workflow_Name, System_Workflow_Description) VALUES (" + nextID + ", '" + txtWorkflowName.Text.Trim() + "', '" + txtWorkflowDescription.Text.Trim() + "');");
            db.close();
            gvWorkflows.DataBind();
        }

        protected void gvWorkflows_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gvWorkflows.Rows[index];
            if (e.CommandName == "Del")
            {
                clearEdit();
                //delete row
                MiniSaaSDB db = new MiniSaaSDB();
                string workflowid = gvWorkflows.DataKeys[index].Value.ToString();
                db.open();
                db.executeNonQuery("DELETE FROM SYSTEM_WORKFLOW_SERVICE WHERE System_Workflow_ID = " + workflowid + ";");
                db.executeNonQuery("DELETE FROM SYSTEM_WORKFLOW WHERE System_Workflow_ID = " + workflowid + ";");
                db.close();
                gvWorkflows.DataBind();
            }
            else if (e.CommandName == "Ed")
            {

                editWorkflow(row.Cells[0].Text, row.Cells[1].Text);
            }
        }

        protected void gvWorkflows_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Del")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = gvServices.Rows[index];
                string serviceid = gvServices.DataKeys[index].Values["Service_ID"].ToString();
                string servicename = row.Cells[1].Text;
                string workflowid = lblWorkflowID.Text;
                MiniSaaSDB db = new MiniSaaSDB();
                db.open();
                db.executeNonQuery("DELETE FROM SYSTEM_WORKFLOW_SERVICE WHERE System_Workflow_ID = " + workflowid + " AND Service_ID = "+serviceid+";");
                db.close();
                ddlServices.Items.Add(serviceid  + ". " + servicename);
                refreshServiceList();
            }
        }


    }
}
